package com.wuxu.lianxi_classloader.loader

import android.os.Build
import dalvik.system.BaseDexClassLoader

/**
 *@author wuxu
 *@date 2024/5/30 15:44
 *@description 这个ClassLoader是将插件包代码合并入当前的宿主进程里
 *
 * 这个classLoader的元素需要通过反射放到原ClassLoader里
 */
class CombineClassLoader(dexPath:String,  parent:ClassLoader?=null ) :
    BaseDexClassLoader(dexPath,null,null,parent){

    override fun loadClass(name: String?, resolve: Boolean): Class<*> {
        var c:Class<*>? = findLoadedClass(name)
        val classNotFoundException = ClassNotFoundException(name)
        if (c == null) {
            try {
                c = super.loadClass(name, resolve)
            } catch (e: ClassNotFoundException) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    classNotFoundException.addSuppressed(e)
                }
            }
        }

        if (c==null){
            throw classNotFoundException
        }
        return c
    }
}